﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	
	import lt.uza.utils.*;
	
	import ProximityManager;

	import Camera2D;
	
	import Volk;	
	import Titan;
	import Aura;
	
	public class Titans extends MovieClip {
		
		//VARS
		private var global:Global = Global.getInstance();
		
		//var pm:ProximityManager = new ProximityManager(48);
		
		var GameTimer:Timer = new Timer(1000/30); //30 fps
		var ent_Titans:Array =new Array();
		var ent_Auras:Array =new Array();
		
		var aura_shapes:Shape = new Shape();
		
		 
		
		var cameraMove:Boolean=false;
		
		var oldmouseX:Number;
		var oldmouseY:Number;
		
		var mouseMode:int=0;//	0=none
							//	1=selection
							//	2=move;
		
		public function Titans():void{
			//INIT
			global.camera=new Camera2D;
			
			global.cameraX=0;
	        global.cameraY=0;
			
			addChild(aura_shapes);
			
			GameTimer.addEventListener(TimerEvent.TIMER, mainGameLoop); //add global gametimer
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
			//INIT TITANS
			
			for(var i:int=0; i<6;i++){
				var t_titan = new Titan();
				ent_Titans.push(t_titan);
				ent_Titans[i].ID=i;
				this.addChild(ent_Titans[i]);
			}			
			
			GameTimer.start();
		}
		
		function mainGameLoop(event:TimerEvent):void{
	
			//TITAN BEHAVIOUR LOOP
			for(var i:int=0;i<ent_Titans.length;i++)
			{
					var o: int = 10;
						//CHECK OF TITAN IN AURA STAAT
						for(var j:int=0;j<ent_Auras.length;j++){
							if(ent_Auras[j].IDparent == ent_Titans[i].ID){
								var tx = Math.abs(ent_Auras[j].positions[0] - ent_Titans[i].x);
								var ty = Math.abs(ent_Auras[j].positions[1] - ent_Titans[i].y);
								var ta = Math.sqrt(tx*tx+ty*ty);
								if(ent_Auras[j].str/2 < ta){
									ent_Auras[j].str-=Math.log(1.09);
								}
								if(ent_Auras[j].str/2 > ta){
									ent_Auras[j].str+=Math.log(1.07);
									o = 15;
								}
							}
						}
						if(o==10){ //ZO NIET MAAK NIEUWE AURA
							var t_Aura = new Aura(ent_Auras.length, 5, i, ent_Titans[i].x, ent_Titans[i].y);
							ent_Auras.push(t_Aura);
							
							//pm.addItem(ent_Auras[ent_Auras.length-1]);
						}

				ent_Titans[i].Update();	
				
				
			}
			//AURA BEHAVIOUR LOOP
			for(var a:int=0;a<ent_Auras.length;a++)
			{
				//ent_Auras[a].positions[0]+=global.cameraX;
				//ent_Auras[a].positions[1]+=global.cameraY;
				//CHECK STRENGTH AURA
				
				ent_Auras[a].Update();
				if(ent_Auras[a].str<1){ //VERWIJDER OUDE AURAS
					//pm.removeItem(ent_Auras[a]);
					ent_Auras.splice(a,1);
					
				}
			}
			UpdateAuraShapes(); 
		}
		
		 function MouseDown(evt:MouseEvent):void{	//MUISKLICK DESTINATION TITANS

			if(mouseMode==1){//mouse mode is TITAN MOVE
				for(var o:int=0;o<ent_Titans.length;o++)
				{
					if(ent_Titans[o].Selected)
					{
						ent_Titans[o].Goto(mouseX,mouseY);
						ent_Titans[o].Idle=false;
					}	
				}
				mouseMode =0;
			}			
			
			if(!mouseMode==1){//mouse mode is CAMERA MOVE 
				mouseMode=2;	
				oldmouseX=mouseX;
	 			oldmouseY=mouseY;
			}

			for(var i:int=0;i<ent_Titans.length;i++) //Check for TITAN SELECT
			{
				ent_Titans[i].Selected=false;
				if(mouseX > (ent_Titans[i].x-10) && mouseX < (ent_Titans[i].x+10) && mouseY > (ent_Titans[i].y-10) && mouseY < (ent_Titans[i].y+10))
					{
					mouseMode=1; 
					ent_Titans[i].Selected=true;

					}
			}
				global.camera.Reset();
		}

		
		 function MouseUp(evt:MouseEvent):void{
			if(mouseMode==2){
				mouseMode=0; 
			}
			 global.camera.Reset();
			 
			
			 
		 }
		 function MouseMove(evt:MouseEvent):void{

			 if(mouseMode==2){	//move the camera if mouse is in move mode
				 
				 global.camera._x=-(oldmouseX-mouseX)/15;
 				 global.camera._y=-(oldmouseY-mouseY)/15;
				 
			 }
			 
			 
		 }
		
		function UpdateAuraShapes():void{
			
			aura_shapes.graphics.clear();
			
			
			//HIER BEN IK DIE SHIT AAN HET FIXEN
			
			
			//ent_Auras.sortOn("IDparent", Array.NUMERIC | Array.DESCENDING);
			//trace(ent_Auras[0].IDparent);
			/*pm.refresh();
			
			for (i=0; i<ent_Auras.length; i++) {
				var neighbors:Array = pm.getNeighbors(ent_Auras[i]);
				var l:uint = neighbors.length;
				for (var j:uint=0; j<l; j++) {
					
				}
			
			}*/
			
			
			for(var i:int=0;i<ent_Auras.length;i++){
				
				aura_shapes.graphics.beginFill(ent_Titans[ent_Auras[i].IDparent].r<<16|ent_Titans[ent_Auras[i].IDparent].g<<8|ent_Titans[ent_Auras[i].IDparent].b);
				aura_shapes.graphics.drawCircle(ent_Auras[i].positions[0], ent_Auras[i].positions[1], ent_Auras[i].str);
				aura_shapes.alpha=0.3;
				
			}
		}
		
	}
}